package com.biz.primus.ms.sms.api;

import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.model.misc.enums.SMSType;
import com.biz.primus.model.misc.vo.*;
import com.biz.primus.ms.sms.service.CMPManageService;
import com.biz.primus.ms.sms.service.SMSService;
import com.biz.primus.ms.sms.service.ShortMessageService;
import com.biz.primus.ms.sms.service.SmsTemplateService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * SmsApiController
 *
 * @author JKLiues
 * @date 2017年10月30日
 */
@RestController
@RequestMapping("soa/misc")
public class SmsApiController {
    private final SMSService smsService;

    private final ShortMessageService shortMessageService;

    private final CMPManageService cmpManageService;

    private final SmsTemplateService smsTemplateService;

    @Autowired
    public SmsApiController(SMSService smsService,
                            ShortMessageService shortMessageService,
                            CMPManageService cmpManageService,
                            SmsTemplateService smsTemplateService) {
        this.smsService = smsService;
        this.shortMessageService = shortMessageService;
        this.cmpManageService = cmpManageService;
        this.smsTemplateService = smsTemplateService;
    }

    @ApiOperation("分页获取短信发送记录")
    @PostMapping("queryRecord")
    public SpringPageVO<SendTaskVo> queryRecord(@RequestBody SendTaskQuery query) {
        return cmpManageService.queryRecord(query);
    }

    @ApiOperation("获取余额")
    @GetMapping("getBalance")
    public long getBalance(@ApiParam(value = "供应商ID") @RequestParam("providerId") String providerId) {
        return cmpManageService.getBalance(providerId);
    }

    @ApiOperation("保存渠道")
    @PostMapping("saveChannel")
    public String saveChannel(@RequestBody ChannelVo channelVo) {
        return cmpManageService.saveChannel(channelVo);
    }

    @ApiOperation("更新渠道")
    @PostMapping("updateChannel")
    public String updateChannel(@RequestBody ChannelVo channelVo) {
        return cmpManageService.updateChannel(channelVo);
    }

    @ApiOperation("更新渠道可用状态")
    @PostMapping("changeChannelState")
    public void changeChannelState(@ApiParam("渠道码") @RequestParam("channelCode") String channelCode,
                                   @ApiParam("状态") @RequestParam("state") boolean state) {
        cmpManageService.changeChannelState(channelCode, state);
    }

    @ApiOperation("获取渠道列表")
    @GetMapping("getChannelList")
    public List<ChannelVo> getChannelList() {
        return cmpManageService.getChannelList();
    }

    @ApiOperation("切换供应商")
    @PostMapping("selectProvider")
    public void selectProvider(@ApiParam(value = "供应商ID") @RequestParam("providerId") String providerId) {
        cmpManageService.selectProvider(providerId);
    }

    @ApiOperation("获取供应商列表")
    @GetMapping("getProviderList")
    public List<ProviderInfoVo> getProviderList() {
        return cmpManageService.getProviderList();
    }

    @ApiOperation("更新敏感词")
    @PostMapping("updateSensitiveWords")
    public void updateSensitiveWords(@RequestBody Set<String> words) {
        cmpManageService.updateSensitiveWords(words);
    }

    @ApiOperation("获取敏感词")
    @GetMapping("getSensitiveWords")
    public Set<String> getSensitiveWords() {
        return cmpManageService.getSensitiveWords();
    }

    @ApiOperation("获取短信模板")
    @GetMapping("allTemplate")
    public List<SmsTemplateVo> allTemplate() {
        return smsTemplateService.allTemplate();
    }

    @ApiOperation("获取短信模板内容")
    @GetMapping("getSmsTemplate")
    public SmsTemplateVo getSmsTemplate(@ApiParam("短信类型") @RequestParam("type") SMSType type) {
        return smsTemplateService.getSmsTemplate(type);
    }

    @ApiOperation("更新短信模板内容")
    @PostMapping("saveOrUpdateSmsTemplate")
    public void saveOrUpdateSmsTemplate(@RequestBody SmsTemplateVo vo) {
        smsTemplateService.saveOrUpdateSmsTemplate(vo);
    }

    @ApiOperation("同步短信模板")
    @PostMapping
    public void sync2Redis() {
        smsTemplateService.sync2Redis();
    }

    @ApiOperation("校验短信验证码（校验通过后删除）")
    @PostMapping("validateAndDisableSMSCode")
    public Boolean validateAndDisableSMSCode(@ApiParam("手机号") @RequestParam("mobile") String mobile,
                                             @ApiParam("短信类型") @RequestParam("type") SMSType type,
                                             @ApiParam("验证码") @RequestParam("smsCode") String smsCode) {
        return smsService.validateAndDisableSMSCode(mobile, type, smsCode);
    }

    @ApiOperation("校验短信验证码（校验通过后删除）")
    @PostMapping("validateSMSCode")
    public Boolean validateSMSCode(@ApiParam("手机号") @RequestParam("mobile") String mobile,
                                   @ApiParam("短信类型") @RequestParam("type") SMSType type,
                                   @ApiParam("验证码") @RequestParam("smsCode") String smsCode) {
        return smsService.validateSMSCode(mobile, type, smsCode);
    }

    @ApiOperation("给指定用户发短信")
    @PostMapping("sendSMS")
    public void sendSMS(@ApiParam("用户名") @RequestParam("username") String username,
                        @ApiParam("短信类型") @RequestParam("type") SMSType type) {
        smsService.sendSMS(username,type);
    }
}